/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.model.daos;

import br.com.model.dbconector.DataBaseConnector;
import br.com.model.entidades.Contato;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author Lucas
 */
public class ContatoDAO extends GenericDAO<Contato> {

    @Override
    public void addEntity(Contato entity) {
        String sql = "Insert into Contatos Values (?,?,?,?)";
        //int toReturn = 0;
        if (entity != null) {
            try {
                PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
                //ps.setInt(1, entity.getId());
                ps.setString(1, entity.getNome());
                ps.setString(2, entity.getEndereco());
                ps.setInt(3, entity.getIdade());
                ps.setString(4, entity.getTelefone());
                ps.executeQuery();
                ps.close();
            } catch (SQLException sqle) {
                System.out.println("Erro ao Inserir Dados: " + sqle.getMessage());
               // toReturn = 0;
            }
        }
        //return toReturn;
    }

    @Override
    public int updateEntity(Contato entity) {
        String sql = "Update Contatos set NOME=?, ENDERECO=?, IDADE=?, TELEFONE=? Where ID=?";
        int toReturn = 0;
        if (entity != null) {
            try {
                PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
                //ps.setInt(1, entity.getId());
                ps.setString(1, entity.getNome());
                ps.setString(2, entity.getEndereco());
                ps.setInt(3, entity.getIdade());
                ps.setString(4, entity.getTelefone());
                ps.setInt(5, entity.getId());
                toReturn = ps.executeUpdate();
                ps.close();
            } catch (SQLException sqle) {
                System.out.println("Erro ao Atualizar Dados: " + sqle.getMessage());
                toReturn = 0;
            }
        }
        return toReturn;
    }

    @Override
    public Contato getEntity(int primaryKey) {
        String sql = "Select * from Contatos Where ID=?";
        Contato toReturn = null;
        try {
            PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
            ps.setInt(1, primaryKey);
            ResultSet rs = ps.executeQuery();
            toReturn = new Contato();
            while (rs.next()) {
                toReturn.setId(rs.getInt(1));
                toReturn.setNome(rs.getString(2));
                toReturn.setEndereco(rs.getString(3));
                toReturn.setIdade(rs.getInt(4));
                toReturn.setTelefone(rs.getString(5));
            }
            rs.close();
            ps.close();
        } catch (SQLException sqle) {
            System.out.println("Erro ao Buscar Dados: " + sqle.getMessage());
            toReturn = null;
        }
        return toReturn;
    }

    @Override
    public int removeEntity(int primaryKey) {
        String sql = "Delete from Contatos Where ID=?";
        int toReturn = 0;
        try {
            PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
            ps.setInt(1, primaryKey);
            toReturn = ps.executeUpdate();
            ps.close();
        } catch (SQLException sqle) {
            System.out.println("Erro ao Excluir Dados: " + sqle.getMessage());
            toReturn = 0;
        }
        return toReturn;
    }

    @Override
    public List<Contato> getAllEntitys() {

        String sql = "Select * from Contatos";
        List<Contato> toReturn = new LinkedList<Contato>();
        try {
            ResultSet rs = DataBaseConnector.getInstance().getConnection().createStatement().executeQuery(sql);
            while (rs.next()) {
                Contato cnt = new Contato();
                cnt.setId(rs.getInt(1));
                cnt.setNome(rs.getString(2));
                cnt.setEndereco(rs.getString(3));
                cnt.setIdade(rs.getInt(4));
                cnt.setTelefone(rs.getString(5));
                toReturn.add(cnt);
            }
            rs.close();
        } catch (SQLException sqle) {
            System.out.println("Erro ao Preencher a Lista de Dados: " + sqle.getMessage());
        }
        return toReturn;
    }
}
